//
//  HNSNProductOrderVC.m
//  ZYManagementSystem
//
//  Created by 星道 on 17/6/14.
//  Copyright © 2017年 焕宁. All rights reserved.
//

#import "HNSNProductOrderVC.h"
#import "HNSNProductListCell.h"
#import "HNSNProductOrderModel.h"
#import "HNSNProductOrderView.h"

@interface HNSNProductOrderVC ()
@property (nonatomic, strong) NSMutableArray <HNSNProductOrderModel *>*DataArr;

//页数
@property (nonatomic, assign) NSInteger Index;
@end

@implementation HNSNProductOrderVC

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"套餐订单";

    //设置页数默认为1
    self.Index = 1;
    //取消滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    //添加占位View
    self.tableView.tableFooterView = [UIView new];
    //设置预估高度
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    //取消灰线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //预注册
    [self.tableView registerNib:[UINib nibWithNibName:[HNSNProductListCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    //下拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self RefreshData];
    }];
    //开始加载
    [self.tableView.mj_footer beginRefreshing];
}





#pragma mark - 网络请求

/**
 网络请求
 */
- (void)RefreshData {

    NSDictionary *Params = @{
                             @"action":[self MgrHNSNProductOrderType:self.HNSNProductOrderType],//根据枚举值拿action参数
                             @"pageNumber":@(self.Index),
                             @"oid"   :[AccountTool shareAccountTool].currentAccount.oid
                             };
    [NetManager Get:LLBaseURL(@"yxhUser.do") Auth:YES Params:Params Success:^(id Obj) {

        //结束刷新
        [self EndRefresh];
        if ([Obj[@"code"] isEqualToNumber:@1000]) {

            //主数据存放的地方
            NSArray *Arr = (NSArray *)Obj[@"t"][@"dataList"];

            //为0零表示没有数据了
            if (Arr.count == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{

                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                });
                return ;
            }
            //取值保存
            [Arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                //保存
                HNSNProductOrderModel *Model = [HNSNProductOrderModel HN_ObjectWithKeyValus:obj];
                Model.withdrawMoney = [Obj[@"t"][@"withdrawMoney"] stringValue];
                [self.DataArr addObject:Model];
            }];
            //页数加1
            self.Index += 1;
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });

        } else {

            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *error) {
        //结束刷新
        [self EndRefresh];
        [HUD showError:NetErr];
    }];
}



/**
 清除小红点
 */
- (void)RefreshDataForRedCycle:(HNSNProductOrderModel *)Model
{

    // 如果没有小红点就直接打开详情
    if ([Model.isViewed isEqualToNumber:@0])
    {
        // 直接打开详情
        HNSNProductOrderView.Order(Model);
        return;
    }

    // 需要去除小红点
    NSDictionary *Params = @{
                             @"selectionId" :Model.ID,
                             @"action"      :@"changeIsViewed"
                             };
    [NetManager Get:LLBaseURL(@"yxhUser.do") Auth:YES Params:Params Success:^(id Obj) {

        if ([Obj[@"code"] isEqualToNumber:@1000])
        {
            // 去除小红点
            Model.isViewed = @0;

            dispatch_async(dispatch_get_main_queue(), ^{
                // 更新表单
                [self.tableView reloadData];
                // 打开详情
                HNSNProductOrderView.Order(Model);
            });

        }
        else
        {
            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {

        [HUD showError:NetErr];
    }];
}



/**
 结束刷新
 */
- (void)EndRefresh{

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
}





#pragma mark - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNSNProductListCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.OrderModel = self.DataArr[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNSNProductOrderModel *Model = self.DataArr[indexPath.row];

    // 清除小红点
    [self RefreshDataForRedCycle:Model];
}




#pragma mark - 事件处理方法
/**
 根据本页枚举值返回网络请求的action参数
 */
- (NSString *)MgrHNSNProductOrderType:(HNSNProductOrderType)HNSNProductOrderType
{
    if (HNSNProductOrderType == HNSNProductOrderSN)
    {
        // SN机构
        return @"productOrderList";
    }
    else if (HNSNProductOrderType == HNSNProductOrderNormal)
    {
        // 普通机构
        return @"packageWithdrawOrderList";
    }
    else
    {
        return @"";
    }
}



#pragma mark - setter
- (NSMutableArray<HNSNProductOrderModel *> *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
